//
//  DetailViewController.swift
//  ColorNote
//
//  Created by 伏文东 on 2018/2/10.
//  Copyright © 2018年 伏文东. All rights reserved.
//

import UIKit

private let font = UIFont.systemFont(ofSize: 16)
class DetailViewController: UIViewController {

    var content: ContentMo!
    
    // MARK: 控件属性
    @IBOutlet weak var scrollView: UIScrollView!
    
    private var contentLabel : UILabel = {
        let label = UILabel()
        label.font = font
        label.textAlignment = .left
        label.textColor = UIColor.navColor()
        label.numberOfLines = 0
        return label
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    // MARK: - 监听方法
    @objc private func clickEdit() {
        let post = PostViewController()
        /// 回调文字 
        post.callBackText = { text in
            self.showContent(text: text)
        }
        post.contentText = content.contentLabel
        post.inquire = content.timestamp
        let nav = UINavigationController(rootViewController: post)
        present(nav, animated: true, completion: nil)
    }
    
}
// MARK: - 设置UI界面
private extension DetailViewController {
    func setupUI()  {
        setupNavigation()
        showContent(text: content.contentLabel!)
        scrollView.addSubview(contentLabel)
    }
    
    func showContent(text: String) {
        let contentRect = contentHeight(string: text)
        contentLabel.frame = CGRect(x: 20, y: 20, width: kScreenW-40, height: contentRect.size.height)
        contentLabel.text = text
    }
    
    func setupNavigation()  {
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .edit, target: self, action: #selector(clickEdit))
    }
    
    // MARK: 内容尺寸
    func contentHeight(string: String) -> CGRect {
        let size = CGSize(width: kScreenW - 40, height: 999999999)
        let attributes = [NSAttributedStringKey.font:font]
        
        let contentRect: CGRect = string.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attributes, context: nil)
        scrollView.contentSize = CGSize(width: contentRect.size.width, height: contentRect.size.height+40)
        return contentRect
    }
}
































